package cn.xu.bookstore.order.dao;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import cn.itcast.commons.CommonUtils;
import cn.itcast.jdbc.TxQueryRunner;
import cn.xu.bookstore.book.domain.Book;
import cn.xu.bookstore.order.domain.Order;
import cn.xu.bookstore.order.domain.OrderItem;

public class OrderDao {
	private QueryRunner qr = new TxQueryRunner();
	
	public void addOrder(Order order){
		String sql ="insert into orders values(?,?,?,?,?,?)";
		Timestamp time = new Timestamp(order.getOrdertime().getTime());
		Object[] params = {order.getOid(),time,
				order.getTotal(),order.getState(),order.getOwner().getUid(),order.getAddress()};
		try {
			qr.update(sql,params);
		} catch (SQLException e) {
			throw new RuntimeException();
		}
		
	}
	
	/*
	 * 批量天玑道数据库中
	 */
	public void addOrderItemList(List<OrderItem> orderItems){
		String sql ="insert into orderitem values(?,?,?,?,?)";
		Object[][] params = new Object[orderItems.size()][];
		for(int i=0;i<orderItems.size();i++){
			OrderItem orderItem = orderItems.get(i); 
			params[i] = new Object[]{orderItem.getIid(),orderItem.getCount(),
					orderItem.getSubtotal(),orderItem.getOrder().getOid(),
						orderItem.getBook().getBid()};
		}
		try {
			qr.batch(sql, params);
		} catch (SQLException e) {
			throw new RuntimeException();
		}
		
		
	}

	
	public List<Order> findByUid(String uid) {
		/*
		 * 根据用户的uid来查询该订单 并形成订单集合
		 * 然后遍历每个订单 并设置每个订单的条目
		 * 和 book对象
		 */
		String sql ="select * from orders  where uid=?";
		List<Order> orderList =null;
		try {
			 orderList = qr.query(sql, new BeanListHandler<Order>(Order.class),uid);
			
			/*
			 * 遍历每个订单 并设置订单的条目
			 */
			for(Order order : orderList){
				//得到该订单的 所有的订单条目
				 loadOrderItems(order);
				
			}
		} catch (SQLException e) {
			throw new RuntimeException();
		}
		
		return orderList;
	}

	/*
	 * 加载该订单的订单条目**
	 * 查处该订单的所有的订单条目
	 * 该订单的条目中两个对象bid和iid
	 * 而不是一个bean 所以不用BeanListHandler,
	 * 而用 MapListHandler 他是获取一条记录
	 */
	private void loadOrderItems(Order order) {
		String sql = "select * from orderitem oitem , book b  where oitem.bid = b.bid and oid=?";
		List<Map<String,Object>> listMap =null;
		
		try {
			listMap = qr.query(sql,new MapListHandler(),order.getOid());
			//将该订单条目map的集合 生成 订单条目集合对象
			//并设置该 订单的订单条目
			 order.setOrderItems(toOrderItemList(listMap));
			 
		} catch (SQLException e) {
			throw new RuntimeException();
		}
	}
	
	
	

	/*
	 * 遍历每条记录 并生成订单
	 * 然后将订单添加到订单条目集合中并返回
	 */
	private List<OrderItem> toOrderItemList(List<Map<String, Object>> listMap) {
		List<OrderItem> orderItems=new ArrayList<OrderItem>();
		for(Map<String,Object> obj: listMap){
			OrderItem orderItem = toOrderItem(obj);
			orderItems.add(orderItem);
		}
		
		return orderItems;
	}

	/*
	 *将每个订单条目 生成订单条目对象和Book对象
	 *然后设置 订单条目属性
	 *最后返回订单条目对象
	 */
	private OrderItem toOrderItem(Map<String, Object> obj) {
		OrderItem orderItem = CommonUtils.toBean(obj, OrderItem.class);
		Book book = CommonUtils.toBean(obj, Book.class);
		orderItem.setBook(book);
		return orderItem;
	}

	public Order findByOid(String oid) {
		String sql = "select * from orders where oid=?";
		
		Order order =null;
		try {
			order = qr.query(sql,new BeanHandler<Order>(Order.class),oid);
			 loadOrderItems(order);
		} catch (SQLException e) {
			throw new RuntimeException();
		}
		return order;
	}
	
	/*
	 * 查询该订单的状态
	 */
	public Integer getStateByOid(String oid){
		String sql = "select * from orders where oid =?";
		int state = 0;
		try {
			state = qr.query(sql, new BeanHandler<Order>(Order.class),oid).getState();
		} catch (SQLException e) {
			throw new  RuntimeException();
		}
		
		return state;
	}
	
	/*
	 * 更新 订单的状态
	 */
	public void updateState(String oid,int state){
		String sql ="update orders set state = ? where oid=?";
		try {
			qr.update(sql,state,oid);
		} catch (SQLException e) {
			throw new RuntimeException();
		}
	}
	

}
